package main.view;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;

import automata.Label;

/**
 * XML Tree exibition
 * @author Aryadne Guardieiro P. Rezende
 */
public class Tree extends DefaultHandler {

	private JTree tree;
	private String documentName;
	private DefaultMutableTreeNode noRoot;
	private  DefaultMutableTreeNode lastNode;

	private void createTree() {
		noRoot = new DefaultMutableTreeNode("xml: "+documentName);
		tree = new JTree(noRoot);
		lastNode = noRoot;
	}

	private void addSon(String leaf) {
		DefaultMutableTreeNode l = new DefaultMutableTreeNode(leaf);
		lastNode.add(l);
		tree = new JTree(noRoot);
		lastNode = l;
	}
	

	
	public void startDocument() {
		// xml = "<?xml version =\"1.0\"?>\n";
		createTree();
	}

	public JTree createTree(String uri)
	{
		SAXParser sp = new SAXParser();
		InputSource source = new InputSource(uri);
		sp.setContentHandler(this);
		try
		{
			documentName = uri;
			sp.parse(source);
		}
		
		catch (Exception e)
		{
			System.out.println("Error Parse Validation");
			e.printStackTrace();
		}
		
		return tree;
	}
	public void endDocument() {
		// System.out.println("elements + attributes: " + counter);
	}

	/**
	 * This function read the start element of tag from xml This function is
	 * implemented of SAXParser
	 * 
	 * @param String
	 *            nameSpaceURI
	 * @param String
	 *            localNome
	 * @param String
	 *            rawName its name of tag XML
	 * @oaram Atributes atters
	 * @return void
	 */
	public void startElement(String namespaceURI, String localName,
			String rawName, Attributes attrs) {
		
		addSon(rawName);
		int numAt = attrs.getLength();
		if (numAt > 0)
		{
			addAttrs(attrs, numAt);
		}
		
	}

	public void addAttrs(Attributes attrs, int numAt)
	{
		
		for(int i = 0; i < numAt; i++)
		{
			DefaultMutableTreeNode l = new DefaultMutableTreeNode("@"+(attrs.getLocalName(i))+" : " + attrs.getValue(i));
			lastNode.add(l);
			tree = new JTree(noRoot);
		}
	}
	
	/**
	 * This function clean the characteres
	 * @return 
	 * 
	 * @return void
	 */
	
	public void ignorableWhitespace(char buffer[], int offset, int length)
	{
		
	}
	
	public void characters(char ch[], int start, int length) {
		String n = new String(ch, start, length);
		if (!white(n))
		{
			DefaultMutableTreeNode l = new DefaultMutableTreeNode("value: "+n);
			lastNode.add(l);
			tree = new JTree(noRoot);			
		}
		else
			return;
		
	}
	
	public boolean white(String n)
	{
		int flag = 0;
		for(int i = 0; i < n.length(); i++)
		{
			char c = n.charAt(i);
			if (c == ' ' || c == '\n' || c == '\t')
				flag++;
		}
		
		if (flag == n.length())
			return true;
		else
			return false;
	}

	/**
	 * This function read the end element of tag from xml This function is
	 * implemented of SAXParser
	 * 
	 * @param String
	 *            nameSpaceURI
	 * @param String
	 *            localNome
	 * @param String
	 *            rawName its name of tag XML
	 * @return void
	 */
	public void endElement(String namespaceURI, String localName, String rawName) {
		lastNode = (DefaultMutableTreeNode) lastNode.getParent();
	}

}
